package com.hsytech.dijiebao.person.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.hsytech.dijiebao.R;
import com.hsytech.dijiebao.base.BaseActivity;
import com.hsytech.dijiebao.base.DialogFragmentListener;
import com.hsytech.dijiebao.base.SimpleDialogFrament;
import com.hsytech.dijiebao.home.alldetail.HotelDetailActivity;
import com.hsytech.dijiebao.utils.SharedPreferencesTools;
import com.hsytech.dijiebao.view.pulltorefresh.PullToRefreshBase;
import com.hsytech.dijiebao.view.pulltorefresh.PullToRefreshListView;
import com.hsytech.dijiebao.view.pulltorefresh.PullToRefreshBase.Mode;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MoreReleaseHelpHotleActivity extends BaseActivity {
	private PullToRefreshListView listview_more_taxi;
	private TextView bar_title_tv, default_text;
	private MoreReleaseHelpHotleAdapter taxiAdapter;
	private List<HashMap<String, String>> listdatat = new ArrayList<HashMap<String, String>>();
	private ImageButton top_back_btn;
	private Button bar_right_top_btn;
	private int firstRow = 0;
	private int listRows = 10;
	private int currentNum = 0;
	private String count;
	private int delatePosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.more_release_help_hotle);
		initView();
		getHttp();
	}

	private void getHttp() {
//		RequestParams data = new RequestParams();
//		data.put("type", 1);
//		data.put("token", SharedPreferencesTools.getTOKEN(this));
//		data.put("firstRow", firstRow);
//		data.put("listRows", listRows);
//		// data.put("token", SharedPreferencesTools.getTOKEN(this));
//		HttpUtils.get("http://djb.dev.hsydev.com/api.php/User/myPublistHotelList", MoreReleaseHelpHotleActivity.this,
//				data, new JsonResponseHandler() {
//					@Override
//					public void onSuccess(JSONObject data) {
//						JSONObject result = data.optJSONObject("result");
//						JSONArray resultData = result.optJSONArray("data");
//						count = result.optString("count");
//
//						for (int i = 0; i < resultData.length(); i++) {
//
//							String cityName = resultData.optJSONObject(i).optString("city_name");
//							String areaName = resultData.optJSONObject(i).optString("area_name");
//							String hotleName = resultData.optJSONObject(i).optString("hotel_cat_name");
//							String name = resultData.optJSONObject(i).optString("name");
//							String startName = resultData.optJSONObject(i).optString("start_time");
//							String endName = resultData.optJSONObject(i).optString("end_time");
//							String id = resultData.optJSONObject(i).optString("id");
//							String create_datetime = resultData.optJSONObject(i).optString("create_datetime");
//							String is_hot = resultData.optJSONObject(i).optString("is_hot");
//							String is_recommend = resultData.optJSONObject(i).optString("is_recommend");
//							String is_new = resultData.optJSONObject(i).optString("is_new");
//							String check_in_num = resultData.optJSONObject(i).optString("check_in_num");
//							HashMap<String, String> taxiList = new HashMap<String, String>();
//							// taxiList = listdatat.get(i);
//							taxiList.put("city", cityName);
//							taxiList.put("id", id);
//							taxiList.put("content",
//									hotleName + " " + startName + "-" + endName + " ��ס" + check_in_num + "��");
//							listdatat.add(taxiList);
//							//
//						}
//						taxiAdapter.notifyDataSetChanged();
//						listview_more_taxi.onRefreshComplete();
//						currentNum = currentNum + 10;
//
//					}
//
//					@Override
//					public void onError(String mess) {
//						// TODO Auto-generated method stub
//						super.onError(mess);
//						showToast(mess);
//						listview_more_taxi.onRefreshComplete();
//					}
//
//					@Override
//					public void onFailure(int statusCode, Throwable throwable) {
//						Log.d("mylog", "" + statusCode);
//						super.onFailure(statusCode, throwable);
//					}
//				});
	}

	private void initView() {
		initTitle();
		initBody();
	}

	private void initBody() {

		listview_more_taxi = (PullToRefreshListView) findViewById(R.id.listview_more_taxi);
		taxiAdapter = new MoreReleaseHelpHotleAdapter(this, listdatat);
		listview_more_taxi.setAdapter(taxiAdapter);
		listview_more_taxi.setMode(Mode.BOTH);
		listview_more_taxi.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
				firstRow = 0;
				listRows = 10;
				listdatat.clear();
				taxiAdapter.notifyDataSetChanged();
				getHttp();
			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
				if (currentNum == 0) {
					listview_more_taxi.onRefreshComplete();
					return;
				}
				if (currentNum >= Integer.valueOf(count)) {
//					showToast("û�и���������");

					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							listview_more_taxi.onRefreshComplete();

						}
					}, 500);
				}
				firstRow = firstRow + listRows;
				getHttp();
			}
		});
		listview_more_taxi.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Intent intent = new Intent(MoreReleaseHelpHotleActivity.this, HotelDetailActivity.class);
				intent.putExtra("idHelp", listdatat.get(position - 1).get("id"));
				intent.putExtra("collectHotle", "1");
				startActivity(intent);
			}
		});
		listview_more_taxi.getRefreshableView().setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1, final int position, long arg3) {
				DialogFragmentListener listener = new DialogFragmentListener() {
					@Override
					public void onPositive() {
						delatePosition = position - 1;
						postHttp();
					}

					@Override
					public void onNegative() {

					}
				};
				SimpleDialogFrament.with(MoreReleaseHelpHotleActivity.this).message("ȷ��ɾ����?").attach(listener)
						.showDialog();

				return true;
			}
		});

	}

	private void postHttp() {
		// ����

//		RequestParams data = new RequestParams();
//		data.put("token", SharedPreferencesTools.getTOKEN(this));
//		data.put("type", 2);
//		data.put("id", listdatat.get(delatePosition).get("id"));
//		HttpUtils.post("http://djb.dev.hsydev.com/api.php/User/delMyPub", this, data, new JsonResponseHandler() {
//			@Override
//			public void onSuccess(JSONObject data) {
//				listdatat.remove(listdatat.get(delatePosition));
//				taxiAdapter.notifyDataSetChanged();
//				Toast.makeText(MoreReleaseHelpHotleActivity.this, "ɾ���ɹ�", Toast.LENGTH_SHORT).show();
//			}
//
//			@Override
//			public void onFailure(int statusCode, Throwable throwable) {
//				super.onFailure(statusCode, throwable);
//				Toast.makeText(MoreReleaseHelpHotleActivity.this, "ɾ��ʧ�� ,��������", Toast.LENGTH_SHORT).show();
//			}
//
//		});
	}

	private void initTitle() {
		top_back_btn = (ImageButton) findViewById(R.id.top_back_btn);
		top_back_btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				finish();
			}
		});
		bar_right_top_btn = (Button) findViewById(R.id.bar_right_top_btn);
		bar_right_top_btn.setVisibility(View.GONE);
		bar_title_tv = (TextView) findViewById(R.id.bar_title_tv);
		bar_title_tv.setText("����Ƶ�");
	}

	public class MoreReleaseHelpHotleAdapter extends BaseAdapter {
		private Context context;
		private List<HashMap<String, String>> listdata;
		private LayoutInflater inflater;
		private View myitemview;

		public MoreReleaseHelpHotleAdapter(Context context, List<HashMap<String, String>> listdata) {
			this.context = context;
			inflater = LayoutInflater.from(context);
			this.listdata = listdata;
		}

		@Override
		public int getCount() {
			return listdata.size();
		}

		@Override
		public Object getItem(int position) {
			return listdata.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View itemView, ViewGroup parent) {

			itemView = inflater.inflate(R.layout.detail_more_hotle, null);
			TextView city = (TextView) itemView.findViewById(R.id.city);
			TextView detail_content = (TextView) itemView.findViewById(R.id.detail_content);
			// ImageView Logo = (ImageView)
			// itemView.findViewById(R.id.item_worker_banklist_image);

			HashMap<String, String> map = listdata.get(position);
			city.setText(map.get("city"));
			detail_content.setText(map.get("content"));
			// isDefault.setTextColor(context.getResources().getColor(Integer.valueOf(map.getB())));
			// holder.t4.setTextColor(context.getResources().getColor(Integer.valueOf(map.get("t5").toString())));

			return itemView;
		}

	}

}
